package com.studyv3.test.controller.Count;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.studyv3.common.core.domain.R;
import com.studyv3.test.domain.Batch;
import com.studyv3.test.domain.Products;
import com.studyv3.test.domain.Stock;
import com.studyv3.test.domain.vo.NumsVo;
import com.studyv3.test.mapper.BatchMapper;
import com.studyv3.test.mapper.CountMapper;
import com.studyv3.test.mapper.ProductsMapper;
import com.studyv3.test.mapper.StockMapper;
import com.studyv3.test.redis.RedisKeyConfig;
import com.studyv3.test.service.CountService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

@RestController
@RequestMapping("api/count")
@RequiredArgsConstructor
public class CountController {

    private final RedisTemplate redisTemplate;

    private final CountService countService;
    @Resource
    CountMapper productCateMapper;
    @Resource
    ProductsMapper productsMapper;
    @Resource
    StockMapper stockMapper;
    @Resource
    BatchMapper batchMapper;

    @GetMapping("/procate")
    public R getCount() {
        return R.ok(countService.cateCount());
    }

    @GetMapping("/nums")
    public R getNums() {
//        if(redisTemplate.hasKey(RedisKeyConfig.CACHE_TYPES)){
//            return R.ok(redisTemplate.opsForHash().values(RedisKeyConfig.CACHE_TYPES));
//        }else {
        List<Batch> batchList = batchMapper.selectList(null);
        List<NumsVo> numsVoList = new ArrayList<>();
        for (Batch batch : batchList) {
            NumsVo numsVo = new NumsVo();
            numsVo.setName(batch.getName());
            List<Products> products1 = productsMapper.selectList(Wrappers.<Products>lambdaQuery().orderByAsc(Products::getId));
            Map<String, Long> map = new LinkedHashMap<>();
            for (Products products : products1) {
                List<Stock> stockList = stockMapper.selectList(Wrappers.<Stock>lambdaQuery()
                        .eq(Stock::getProductsId, products.getId())
                        .eq(Stock::getBatchId, batch.getId()));
                if (stockList.size()>0) {
                    Long num =0L;
                    for (Stock stock : stockList) {
                        num+=stock.getProductsNum();
                    }
                    map.put(products.getName(), num);
                } else {
                    map.put(products.getName(), 0L);
                }
            }
            numsVo.setMap(map);
            numsVoList.add(numsVo);
//            Map<String,NumsVo> cacheMap=new HashMap<>();
//            numsVoList.forEach(c->{
//                cacheMap.put(c.getName(),c);
//            });
//            redisTemplate.opsForHash().putAll(RedisKeyConfig.CACHE_TYPES,map);
        }
        return R.ok(numsVoList);}
//    }
}
